package com.cws.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cws.enmu.RspCode;
import com.cws.frame.exception.CWSException;

/**
 * 加解密类
 * 
 * @author Caspar
 * 
 */
public class EncUtil {

	private static final Logger log = LoggerFactory.getLogger(EncUtil.class);

	// AES加密
	public static String aesEnc(String src, String key) {

		// 判断key不为null并且长度为16
		if (key == null || key.length() != 16) {
			log.error("the enc key is null or len not 16");
			throw new CWSException(RspCode.ENCRYPT_ERROR);
		}

		try {
			byte[] raw = key.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(
					"0102030405060708".getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			byte[] encrypted = cipher.doFinal(src.getBytes());
			return EncUtil.byte2hex(encrypted).toLowerCase();
		} catch (Exception e) {
			log.error("AES enc error.", e);
			throw new CWSException(RspCode.ENCRYPT_ERROR);
		}
	}

	// AES解密
	public static String aesDec(String encStr, String key) {

		// 判断key不为null并且长度为16
		if (key == null || key.length() != 16) {
			log.error("the enc key is null or len not 16");
			throw new CWSException(RspCode.ENCRYPT_ERROR);
		}
		try {
			byte[] raw = key.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(
					"0102030405060708".getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] encrypted1 = hex2byte(encStr);

			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original);
			return originalString;

		} catch (Exception e) {
			log.error("AES dec error.", e);
			throw new CWSException(RspCode.ENCRYPT_ERROR);
		}
	}

	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}

	public static byte[] hex2byte(String strhex) {
		if (strhex == null) {
			return null;
		}
		int l = strhex.length();
		if (l % 2 == 1) {
			return null;
		}
		byte[] b = new byte[l / 2];
		for (int i = 0; i != l / 2; i++) {
			b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
					16);
		}
		return b;
	}

	public static String toSha256(String strSrc) {
		MessageDigest md = null;
		String strDes = null;

		byte[] bt = strSrc.getBytes();
		try {
			md = MessageDigest.getInstance("SHA-256");
			md.update(bt);
			strDes = byte2hex(md.digest());
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		return strDes;
	}
}
